package com.neusoft.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.domain.Category;
import com.neusoft.domain.Employee;
import com.neusoft.domain.ResultCode;
import com.neusoft.domain.ResultPage;
import com.neusoft.exception.BusinessException;
import com.neusoft.mapper.CategoryMapper;
import com.neusoft.mapper.FoodMapper;
import com.neusoft.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultPage<Category> findCategoryByPage(Integer page, Integer pageSize, String categoryName,Integer businessId) {
        if(page==null || pageSize == null){
            throw new BusinessException(ResultCode.CATEGORY_PAGE,"没有提供当前页或每页显示条数");
        }

        String key = "category_"+page+"_"+pageSize;
        if(!StringUtils.isEmpty(categoryName)){
            key += "_"+categoryName;
        }
        if(businessId != null){
            key += "_"+businessId;
        }
        // 获取redis中的缓存数据,如果有,直接返回,如果没有继续往下执行
        if(redisTemplate.opsForValue().get(key) != null){
           return (ResultPage<Category>) redisTemplate.opsForValue().get(key);
        }

        PageHelper.startPage(page,pageSize);

        List<Category> categoryList =  categoryMapper.selectCategoryByCondition(categoryName,businessId);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);

        ResultPage<Category> categoryResultPage = new ResultPage<>(pageInfo.getList(), pageInfo.getTotal());

        // 把查询到的数据存入redis
        redisTemplate.opsForValue().set(key,categoryResultPage);

        return categoryResultPage;
    }

    @Override
    public void saveCategory(Category category) {
        categoryMapper.insertCategory(category);
        //清除缓存
        for (Object key : redisTemplate.keys("category_*")) {
            redisTemplate.delete(key);
        }

    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.updateCategoryById(category);

        //清除缓存
        for (Object key : redisTemplate.keys("category_*")) {
            redisTemplate.delete(key);
        }
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        // select count(*) from food where categoryId=?
        if(categoryId == null){
            throw new BusinessException(ResultCode.CATEGORY_GET,"id不能为空");
        }

        Integer count = foodMapper.selectCountByCategoryId(categoryId);

        if(count>0){
            throw new BusinessException(ResultCode.FOOD_GET_CATEGOTYID,"该分类下有菜品，不能删除");
        }

        // 执行分类删除
        categoryMapper.deleteById(categoryId);

        //清除缓存
        for (Object key : redisTemplate.keys("category_*")) {
            redisTemplate.delete(key);
        }
    }
}
